package com.example.petstore.controller;

import com.example.petstore.common.CommonResponse;
import com.example.petstore.domain.Item;
import com.example.petstore.domain.Product;
import com.example.petstore.service.ItemService;
import com.example.petstore.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class ItemController {
    @Autowired
    private ItemService itemService;
    @Autowired
    private ProductService productService;

    /**
     * 进入item页面
     * @param itemId
     * @param model
     * @return
     */
    @GetMapping("/itemForm")
    public String showItem(String itemId, ModelMap model){
        CommonResponse commonResponse = itemService.getItemByItemid(itemId);
        if(commonResponse.isSuccess()){
            Item item = (Item) commonResponse.getData();
            model.addAttribute("item", item);
            Product product = productService.getProductByProductId(item.getProductid());
//            System.out.println(product);
            model.addAttribute("product", product);
//            System.out.println(commonResponse.getData());
//            到item详情页面
            itemService.addViews(itemId);
            return "catalog/item";
        }
//        到首页
        return "catalog/main";
    }
}
